import {Router} from 'vue-router'
import {IMenu, MenuType} from '#/menu'
import router from '@/router'
import {ref} from 'vue'

class Menu {
  private router: Router = router
  public menu = ref<IMenu[]>([])

  getMenus() {
    const routes = router
        .getRoutes()
        .filter(route => route.meta.menu && route.children.length)
        .map(route => {
          const children = [] as MenuType[]
          if (route.meta.menu) {
            route.children.forEach(croute => {
              if (croute.meta?.menu?.public) {
                croute.meta.menu.public.forEach(cc => {
                  children.push(cc)
                })
                return
              }
              const cmenu = {
                path: route.path + croute.path,
                title: croute.meta?.menu?.title!,
              }
              children.push(cmenu)
            })
          }
          return {
            title: route.meta.menu?.title,
            icon: route.meta.menu?.icon,
            children: children,
          }
        })
    this.menu.value = routes as IMenu[]
  }
}

const menuService = new Menu()
export default menuService
